package com.example.drawing.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.drawing.R;

public class MobileView extends View {
    private Paint mPaint;  //绘制线条的Path
//    private Canvas mCanvas;  //内存中创建的Canvas
    private Bitmap mBitmap;  //缓存绘制的内容
    private int maxX;
    private int maxY;
    private int mLastX;
    private int mLastY;

    public int getMaxX() {
        return maxX;
    }

    public void setMaxX(int maxX) {
        this.maxX = maxX;
    }

    public int getMaxY() {
        return maxY;
    }

    public void setMaxY(int maxY) {
        this.maxY = maxY;
    }

    public MobileView(Context context) {
        super(context);
        init();
    }

    public MobileView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MobileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MobileView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mPaint = new Paint();   //初始化画笔
        mPaint.setColor(Color.RED);//画笔颜色
        mPaint.setAntiAlias(true);//画笔抗锯齿
        mPaint.setDither(true);//画笔防抖动。
        mPaint.setStyle(Paint.Style.STROKE);//画笔模式为填充
        mPaint.setStrokeJoin(Paint.Join.ROUND); //结合处为圆角
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 设置转弯处为圆角
        mPaint.setStrokeWidth(10);   // 设置画笔宽度
    }

    @Override
    //当子View的父控件要放置该View的时候，父控件会传递两个参数给View——widthMeasureSpec和heightMeasureSpec。
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width =maxX= getMeasuredWidth();//获取宽度
        int height =maxY= getMeasuredHeight();//获取宽度
        // 初始化bitmap,Canvas
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//根据屏幕创建位图
//        mCanvas = new Canvas(mBitmap);//根据位图创建缓存画板
        mLastX = width / 2;
        mLastY = height / 2;
    }



    //重写该方法，在这里绘图
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(mLastX, mLastY, 30, mPaint);
//        canvas.drawBitmap(mBitmap, 0, 0, null);//绘制
    }

    public void mibile(int x, int y) {
        mLastX = mLastX + x;
        mLastY = mLastY + y;
        if (mLastX<=0){
            mLastX=0;
        }else if (mLastX>maxX){
            mLastX=maxX;
        }
        if (mLastY>=maxY){
            mLastY=maxY;
        }else if (mLastY<=0){
            mLastY=0;
        }
        invalidate();//刷新屏幕
    }
}